package org.coderk.mybatis;

import org.apache.ibatis.session.SqlSessionFactory;
import org.coderk.mybatis.mapper.interceptor.ExamplePlugin;
import org.codert.wxpay.annotation.EnableWxPay;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.sql.Statement;
import java.util.concurrent.TimeUnit;

/**
 * @author: TanJingyu
 * @create: 2021-03-21 20:58
 **/
@SpringBootApplication
@EnableAsync
@MapperScan(basePackages = "org.coderk.mybatis.mapper")
@EnableWxPay
public class MybatisApplication {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    private static Integer cpuNums = Runtime.getRuntime().availableProcessors();
//    private ApplicationContext applicationContext;

//    public MybatisApplication(ApplicationContext applicationContext) {
//        this.applicationContext = applicationContext;
//    }

    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }

    /**
     * 在SpringApplication中，callRunners(context, applicationArguments);
     * 会从容器中取出所有的ApplicationRunner和CommandLineRunner，并运行
     * 这时候web服务已经启动好了
     */
//    @Bean
//    public ApplicationRunner codertApplicationRunner() {
//        return args -> {
//            sqlSessionFactory.getConfiguration().addInterceptor(new ExamplePlugin());
//        };
////        return args -> {
//////            TimeUnit.SECONDS.sleep(100);
////            System.out.println("codertApplicationRunner-------------------");
////        };
//    }
//    @Bean
//    public CommandLineRunner codertCommandLineRunner() {
//        return args -> {
//            System.out.println("codertCommandLineRunner------------------");
//        };
//    }

    /**
     * spring重写的线程池，把jdk的Executor重新继承换了个自己的名称，TaskExecutor,然后spring自己的线程相关都实现这个接口
     * 可能是为了跟jdk的做区分吧，也可能是spring认为叫TaskExecutor更符合线程意义
     */
//    @Bean
//    public ThreadPoolTaskExecutor threadPoolTaskExecutor(TaskExecutorBuilder builder) {
//        return builder
//                .queueCapacity(500)
//                .corePoolSize(2)
//                .maxPoolSize(cpuNums +1)
//                .threadNamePrefix("sanjicaigou-")
//                .build();
//    }

}
